﻿using DevComponents.DotNetBar.SuperGrid;
using SDATA;
using SDLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SABI
{
    public partial class frm0096 : Form
    {
        string sql = "";
        string _InventoryID = "";

        public frm0096()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            (Application.OpenForms["frmHome"] as frmHOME).CloseTab(this);
        }

        public void loadDATA_List() 
        {
            sql = "SELECT InventoryTypeID ,InventoryTypeID + ' - ' + InventoryTypeName as InventoryTypeName FROM tb0053 ORDER BY InventoryTypeName";
            DataTable dtT, dt_lst = FRM.GetDataTable(sql);
            dtT = dt_lst.Clone();
            dtT.Rows.Add("%", "%  - Tất cả");
            dtT.Merge(dt_lst);
            lstViewInventoryTypeID.DataSource = dtT;
            lstViewInventoryTypeID.SelectedIndex = 0;
        }

        public void loadDATA_Grid1()
        {
            sql = "SELECT * FROM vi0124 WHERE CHARINDEX('" + FRM.ID_Group + "',GroupShow)>0 AND DivisionID='" + FRM.DivisionID + "' ";
            if (!chkDisabled.Checked) { sql += "AND Disabled=0 "; }
            if (lstViewInventoryTypeID.SelectedIndex > 0) {
                sql += "AND InventoryTypeID='" + lstViewInventoryTypeID.SelectedValue.ToString() + "' "; 
            }
            sql +="ORDER BY InventoryID";
            GridDB1.PrimaryGrid.DataSource = FRM.GetDataTable(sql);
            GridDB1.Update();
            GridDB1.PrimaryGrid.Footer.Text = string.Format("Tổng số dòng: {0:#,#}", GridDB1.PrimaryGrid.Rows.Count);
        }

        private void lstViewInventoryTypeID_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadDATA_Grid1();
        }

        private void chkDisabled_CheckedChanged(object sender, EventArgs e)
        {
            loadDATA_Grid1();
        }

        private void GridDB1_CellDoubleClick(object sender, GridCellDoubleClickEventArgs e)
        {
            if (GridDB1.PrimaryGrid.VisibleRowCount == 0) { return; }
            nEdit();
        }

        void nAdd()
        {
            Form frm = new frm0106(true, false, "");
            frm.ShowDialog();
        }
        void nEdit()
        {
            Form frm = new frm0106(false, false, _InventoryID);
            frm.ShowDialog();
        }
        void nDelete()
        {
            if (GridDB1.PrimaryGrid.GetSelectedRows().Count < 1)
            {
                SDLL.DLL02.Alert("Bạn phải chọn dòng cần xóa!");
                return;
            }
            if (!SDLL.DLL02.Confirm("Bạn có chắc muốn xóa?", "Xác nhận")) { return; }

            sql = string.Format("Select * from tb0054 Where InventoryID='{0}'", _InventoryID);
            DataTable dt = DATA.GetDataTable(sql,FRM.ConnectString);

            // mặt hàng không tồn tại ---> bỏ qua
            if(dt.Rows.Count==0){return;}

            sql = string.Format("pr0078 '{0}'", _InventoryID);
            DataTable dt2 = FRM.GetDataTable(sql);
            if(dt2.Rows[0]["Status"].ToString()=="1")
            {
                SDLL.DLL02.Alert("Đối tượng bị ràng buộc, không thể xóa!");
                return;
            }

            sql = string.Format("DELETE tb0054 WHERE InventoryID='{0}'", _InventoryID);
            DATA.ExecuteNonQuery(sql, FRM.ConnectString);
            //sql = string.Format("DELETE tb0063 WHERE InventoryID='{0}'", _InventoryID);
            //DATA.ExecuteNonQuery(sql, FRM.connectString);
            sql = string.Format("DELETE tb0106 WHERE InventoryID='{0}'", _InventoryID);
            DATA.ExecuteNonQuery(sql, FRM.ConnectString);

            loadDATA_Grid1();
            DLL02.Info("Xóa dữ liệu thành công ^^!");
            
        }
        void nView()
        {
            Form frm = new frm0106(false, true, _InventoryID);
            frm.ShowDialog();
        }
        void nInfo()
        {
            string tn = "tb0054";
            string ws = string.Format("WHERE InventoryID='{0}'", _InventoryID);
            Form frm = new frm0067(tn, ws);
            frm.ShowDialog();
        }

        void nRefresh()
        {
            loadDATA_List();
            lstViewInventoryTypeID.SelectedIndex = 0;
        }

        private void GridDB1_SelectionChanged(object sender, GridEventArgs e)
        {
            GridRow gr = (GridRow)e.GridPanel.ActiveRow;
            _InventoryID = gr.Cells["clInventoryID"].Value.ToString();

        }

        private void frm0096_Load(object sender, EventArgs e)
        {

        }

        private void frm0096_Shown(object sender, EventArgs e)
        {
            for (int i = 0; i < GridDB1.PrimaryGrid.Columns.Count;i++ )
            {
                GridDB1.PrimaryGrid.Columns[i].FilterMatchType = FilterMatchType.Wildcards;
            }
            loadDATA_List();
        }

        private void GridDB1_RowDoubleClick(object sender, GridRowDoubleClickEventArgs e)
        {

        }

        private void mAdd_Click(object sender, EventArgs e)
        {
            nAdd();
        }

        private void mEdit_Click(object sender, EventArgs e)
        {
            nEdit();
        }

        private void mDelete_Click(object sender, EventArgs e)
        {
            nDelete();
        }

        private void mView_Click(object sender, EventArgs e)
        {
            nView();
        }

        private void mRefresh_Click(object sender, EventArgs e)
        {
            nRefresh();
        }

        private void mSystemInfo_Click(object sender, EventArgs e)
        {
            nInfo();
        }
    }
}
